package com.community.service.impl;


import com.community.service.WorkbenchService;
import com.community.service.MemberService;
import com.community.service.OwnerService;
import com.lening.common.thread.ThreadGroup;
import com.lening.common.web.Request;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;


@Service
public class WorkbenchImpl implements WorkbenchService {

    @Resource
    private OwnerService ownerService;
    @Resource
    private MemberService memberService;
    @Override
    public Map<String, Object> getWorkbenchData() throws ExecutionException, InterruptedException {
//        ExecutorService threadPool = ThreadGroups.fixed;
//        Object complain = CompletableFuture.supplyAsync(() -> Request.syncGet("community-management/property/complain/getStateFalseCount", Object.class), threadPool).join();

//        //投诉未处理个数
        Object complain = Request.asyncGet("property/complaSuggestionMainTables/count", Object.class);
//        //待处理报修个数
        Object repair = Request.asyncGet("property/ownerRepairMainTables/count", Object.class);
//        待处理工单个数
        Object work = Request.asyncGet("property/ernalWorkOrderMainTables/count", Object.class);
//        //待审核业主
//        Object owner = Request.asyncGet("community-management/owner/owner/getStateFalseCount", Object.class);
//        //待审核家属
//        Object family = Request.asyncGet("community-management/owner/family-members/getStateFalseCount", Object.class);

        //社区个数
        Object community = Request.asyncGet("community-management/community/getCount", Object.class);
        //楼宇个数
        Object floor = Request.asyncGet("community-management/floor/getCount", Object.class);
        //房屋个数
        Object house = Request.asyncGet("community-management/house/getCount", Object.class);
        //商铺个数
        Object shop = Request.asyncGet("community-management/shop/getCount", Object.class);
        //车位个数
        Object stall = Request.asyncGet("community-management/stall/getCount", Object.class);

        Map<String,Object> result = new HashMap<>();
       result.put("complain",complain);
        result.put("repair",repair);
        result.put("work",work);
//        result.put("owner",owner);
//        result.put("family",family);

        result.put("community",community);
        result.put("floor",floor);
        result.put("house",house);
        result.put("shop",shop);
        result.put("stall",stall);
        return result;

    }
    @Override
    public Map getAll() {
        //使用线程池执行
        ExecutorService threadPool = ThreadGroup.FIXED_THREAD_POOL;

        /**
         * 异步获取待审核业主个数
         */
        CompletableFuture owner= CompletableFuture.supplyAsync(() ->{
            return ownerService.getCount();
        },threadPool);
        CompletableFuture member= CompletableFuture.supplyAsync(() ->{
            return memberService.getCount();
        },threadPool);


        //等待线程执行完成
        CompletableFuture.allOf(owner,member).join();


        HashMap<Object,Object> result  = new HashMap<>();

        try {
            result.put("owner",owner.get());
            result.put("member",member.get());

        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;

    }
}
